import Taro from '@tarojs/taro'
import app from '../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            id: this.$router.params.id,
            case_course: {},
            course_items: []
        };
        return this.state;
    },
    getCourse() {
        var me = this;
        app.get('api/case_details/getCourse', {id: this.state.id}, function(res) {
            var result = res.data.result;
            var course_items = me.formatCourse(JSON.parse(result.result), JSON.parse(result.course_template.content));
            console.log('course_items',course_items);
            me.setState({
                case_course: result,
                course_items: course_items
            });
        });
    },
    formatCourse(answer, course_template) {
        var course_items = [];
        var subjects = answer.subjects;
        for (var key in subjects) {
            var subject = subjects[key];
            for (var i = 0; i < course_template.length; i++) {
                var page = course_template[i];
                var tp_subjects = page.subjects;
                for (var j = 0; j < tp_subjects.length; j++) {
                    var tp_subject = tp_subjects[j];
                    if (key == tp_subject.id) {
                        var item = {};
                        item.id = key;
                        item.type = tp_subject.type;
                        item.label = tp_subject.label;
                        item.value = subject.value;
                        item.is_privacy = tp_subject.is_privacy ? tp_subject.is_privacy : 0;
                        if (item.type == '眼科检查') {
                            item.displays = subject.displays;
                        }
                        if (item.type == '矩阵填空题') {
                            item.items = tp_subject.items;
                        }
                        if (item.type == '上传图片') {
                            item.items = tp_subject.items;
                        }
                        if (item.type == '矩阵单选题') {
                            item.xItems = tp_subject.xItems;
                            item.yItems = tp_subject.yItems;
                        }
                        course_items.push(item);
                    }
                }
            }
        }

        for (var k = 0; k < course_items.length; k++) {
            var item = course_items[k];
            var type = item.type;
            var value = item.value;
            if (type == '眼科检查') {
                var displays = item.displays;
                var answer = [];
                for (var key in displays) {
                    var yItem = [];
                    yItem.push(key);
                    var left = [];
                    var right = [];
                    for (var y = 0; y < 4; y++) {
                        var lv = value['v1'][key]['v'+(y+1)];
                        if (lv != '' && lv != undefined && lv != null) {
                            left.push(lv);
                        }
                        var rv = value['v2'][key]['v'+(y+1)];
                        if (rv != '' && rv != undefined && rv != null) {
                            right.push(rv);
                        }
                    }
                    yItem.push(left.join(','));
                    yItem.push(right.join(','));
                    answer.push(yItem);
                }
                item.answer = answer;
            }
            if (type == '矩阵单选题') {
                var yItems = item.yItems;
                var xItems = item.xItems;
                var first = [''].concat(xItems);
                var answer = [];
                answer.push(first);
                for (var i = 0; i < yItems.length; i++) {
                    var yItem = [];
                    yItem.push(yItems[i]);
                    var v = value['v'+(i+1)];
                    for (var j = 0; j < xItems.length; j++) {
                        if (v == xItems[j]) {
                            yItem.push(v);
                        }
                         else {
                            yItem.push('');
                        }
                    }
                    answer.push(yItem);
                }
                item.answer = answer;
            }
            if (type == '多选题') {
                var answer = '';
                if (value.v1 instanceof Array) {
                    answer = value.v1.join(',');
                }
                if (value.v2) {
                    answer += answer ? ','+value.v2 : value;
                }
                item.answer = answer;
            }
            if (type == '单选题') {
                var answer = value.v1;
                if (value.v2) {
                    answer += answer ? ','+value.v2 : value;
                }
                item.answer = answer;
            }
            if (type == '填空题') {
                item.answer = value;
            }
            if (type == '矩阵填空题') {
                var answer = [];
                var items = item.items;
                for (var i = 0; i < items.length; i++) {
                    var one = {};
                    one.label = items[i]['label'];
                    one.value = value['v'+(i+1)]
                    answer.push(one);
                }
                item.answer = answer;
            }
            if (type == '上传图片') {
                var answer = [];
                var items = item.items;
                for (var i = 0; i < items.length; i++) {
                    var one = {};
                    one.label = items[i];
                    one.value = this.getValueImage(value, i);
                    answer.push(one);
                }
                item.answer = answer;
            }
        }
        return course_items;
    },
    isInArray(value, array) {
        for (var i = 0; i < array.length; i++) {
            if (value == array[i]) {
                return true;
            }
        }
        return false;
    },
    getValueImage(obj, i) {
        var v = obj['v'+(i+1)];
        if (v == undefined || v.length <= 0) {
            return app.getPath('/image/default_image.png');
        }
        return app.getPath(v[0]['url']);
    },
    componentWillMount () {
        this.init();
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        if(names.length > 1) {
            temp[names[i]] = v;
            this.setState({n: temp});
        } else {
            this.setState({n: v});
        }
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
